package priv.blackbibo.application;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import priv.blackbibo.application.R;
import priv.blackbibo.application.activity.MainActivity;
import priv.blackbibo.application.service.GosonService;
import priv.blackbibo.application.service.VersionService;

/**
 * Created by black on 2019-12-10.
 */
public class BootstrapActivity extends AppCompatActivity {

    private static final String TAG = "BootstrapActivity";

    Handler handler = new Handler();

    private static BootstrapActivity bootstrapActivity;

    private static Configuration configuration;

    private View confirmBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bootstrap);
        confirmBtn = findViewById(R.id.confirm_btn);
        confirmBtn.setVisibility(View.INVISIBLE);
        bootstrapActivity = this;
        configuration = getConfiguration(this);
        checkVersion();
    }

    public static void stepIntoMain(){
        long duration = 500;
        if (configuration.isNew()) {
            duration = 3000;
        }

        try {
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        bootstrapActivity.showBtn();
    }

    public void showBtn(){
        handler.post(()->{
            confirmBtn.setOnClickListener((v) -> {
                gotoNext();
            });
            confirmBtn.setVisibility(View.VISIBLE);
            Animation  alpha= new AlphaAnimation(0, 1);
            alpha.setDuration(500);
            alpha.setStartOffset(0);
            confirmBtn.startAnimation(alpha);
        });
    }

    private void gotoNext() {
        Intent no_sound = new Intent(bootstrapActivity, GosonService.class);
        startService(no_sound);

        Intent intent = new Intent(bootstrapActivity, MainActivity.class);
        startActivity(intent);
    }

    public void toast(){
        handler.post(()->
                Toast.makeText(bootstrapActivity, "请更新值最新版本！", Toast.LENGTH_LONG).show()
        );
    }

    public static void callShowVersionChanged(String url,String versionShort){
        bootstrapActivity.showVersionChanged(url,versionShort);
    }

    public void checkVersion(){
        Intent intent = new Intent(this, VersionService.class);
        startService(intent);
    }

    public void showVersionChanged(final String url,final String versionShort){
        toast();
        handler.post(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(bootstrapActivity);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setTitle("版本更新");
                builder.setMessage("请更新至最新版本 v"+versionShort);
                builder.setPositiveButton(getString(R.string.confirm_to_update), (dialog, which) -> {
                    Intent intent= new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(url);
                    intent.setData(content_url);
                    startActivity(intent);
                });
                builder.setNegativeButton(getString(R.string.ignore_confirm_to_update), (dialog, which) -> {

                    stepIntoMain();
                    Toast.makeText(bootstrapActivity, "别忘记更新了哦", Toast.LENGTH_LONG).show();
                });
                builder.setCancelable(false);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }

    private Configuration getConfiguration(Context context) {
        return new Configuration(bootstrapActivity);
    }

    class Configuration{
        private boolean isNew = false;

        private SharedPreferences sharedPreferences;

        public Configuration(Context context) {
            sharedPreferences = context.getSharedPreferences(getString(R.string.key_configuration), Context.MODE_PRIVATE);
            this.isNew = sharedPreferences.getBoolean(getString(R.string.config_key_new),false);
        }

        public boolean isNew() {
            return isNew;
        }

        public void setNew(boolean aNew) {
            isNew = aNew;
            sharedPreferences.edit().putBoolean(getString(R.string.config_key_new), aNew).apply();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        Intent intent = new Intent(this, MainActivity.class);
//        startActivity(intent);
        Log.d(TAG, "onResume: ");
//        finish();
    }
}
